﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InsuranceSell.Code.Entities;
using System.Data.SqlClient;
using System.Data;

namespace InsuranceSell.Code.Data
{
    class FindCustomerDAO:BaseDAO
    {
        public DataTable SearchCustomer(FindCustomer findCustomer)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("sp_FindCustomer", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@Date", findCustomer.date));
                switch (findCustomer.between) {
                    case 0: command.Parameters.Add(new SqlParameter("@Between", findCustomer.date)); break;
                    case 7: command.Parameters.Add(new SqlParameter("@Between", findCustomer.date.AddDays(7))); break;
                    case 30: command.Parameters.Add(new SqlParameter("@Between", findCustomer.date.AddMonths(1))); break;
                    default: command.Parameters.Add(new SqlParameter("@Between", findCustomer.date)); break;
                }

                SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
                DataTable allCustomers = new DataTable();

                try
                {
                    connection.Open();
                    sqlAdapter.Fill(allCustomers);
                    return allCustomers;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
    }
}
